package treeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution11 {
    //前序遍历 按照访问根节点——左子树——右子树的方式遍历这棵树
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<Integer>();
       preorderTraversal(root,ret);
       return  ret;
    }
   private void preorderTraversal(TreeNode root,List<Integer> fathers){
     if(root!=null){
         fathers.add(root.val);
         if(root.left!=null){
          preorderTraversal(root.left,fathers);
         }
         if(root.right!=null) {
            preorderTraversal(root.right, fathers);
         }
     }

   }

    public static void main(String[] args) {
        /**
         *                 1
         *              /     \
         *             2        3
         *            / \      /  \
         *          3   4     5    6
         *       / \   / \   / \   / \
         *      5  2  3  4  4   3  2 7
         */
        TreeNode root = TreeNodeUtils.getTestTreeNode();
        Solution11 solution11 = new Solution11();
        System.out.println(solution11.preorderTraversal(root));
    }
}
